﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Linq;
using System.Collections.Generic;
using Microsoft.SharePoint;
using System.Globalization;


namespace Presentation.HotSite.WebPart.VWPListProgramacao
{
    public partial class VWPListProgramacaoUserControl : UserControl
    {
        SPSite mySite = SPContext.Current.Site;
        string infoProgramacao = @"<DIV class=""infoProgramacao""><DIV class=""horaInfoProgramacao"">Horário</DIV><DIV class=""progInfoProgramacao"">Programação</DIV></DIV>";

        protected void Page_Load(object sender, EventArgs e)
        {
            SPWeb myWeb = mySite.OpenWeb();
            SPList myList = myWeb.Lists[Data.HotSite.Properties.SharepointStrings.ListaProgramacao]; // "Programacao"];
            CultureInfo culture = new CultureInfo("pt-BR");
            DateTimeFormatInfo dtfi = culture.DateTimeFormat;
            string htmlProgramacao = string.Empty;

            var resultGroupProgramacao = from item in myList.Items.Cast<SPListItem>() orderby Convert.ToDateTime(item["Horário de Início"]).Date ascending, item["Endereço"].ToString() ascending group item by new { Data = Convert.ToDateTime(item["Horário de Início"]).Date, Endereco = item["Endereço"].ToString() } into i select new { i.Key.Data, i.Key.Endereco };

            foreach (var itemGroupProgramacao in resultGroupProgramacao)
            {
                htmlProgramacao += "<div class='headerProgramacao'>Data: " + Convert.ToDateTime(itemGroupProgramacao.Data).Day + " de " + culture.TextInfo.ToTitleCase(dtfi.GetMonthName(Convert.ToDateTime(itemGroupProgramacao.Data).Month)) + ", " + itemGroupProgramacao.Endereco.Substring(itemGroupProgramacao.Endereco.IndexOf("#") + 1) + "</div>";
                htmlProgramacao += infoProgramacao;

                var resultProgramacao = myList.Items.Cast<SPListItem>().Where(i => Convert.ToDateTime(i["Horário de Início"]).Date.Equals(itemGroupProgramacao.Data) && i["Endereço"].ToString().Equals(itemGroupProgramacao.Endereco)).OrderBy(i => i["Horário de Início"]).ToList();
                foreach (SPListItem intemProgramacao in resultProgramacao)
                {
                    htmlProgramacao += "<div class='itemProgramacao'>";
                    htmlProgramacao += "<div class='dataProgramacao'>" + Convert.ToDateTime(intemProgramacao["Horário de Início"]).ToShortTimeString() + " às " + Convert.ToDateTime(intemProgramacao["Horário Final"]).ToShortTimeString() + "</div>";
                    htmlProgramacao += "<div class='descricaoProgramacao'><strong>" + intemProgramacao["Título"] + "</strong>";
                    htmlProgramacao +=  intemProgramacao["Descricao"] != null ? intemProgramacao["Descricao"].ToString() : "";
                    htmlProgramacao += "</div>";
                    htmlProgramacao += "</div>";
                }
            }

            BoxResult.InnerHtml = htmlProgramacao;
        }
    }
}
